<?php
include_once 'app/until/GetData.php';
include_once 'app/modul/db/userConnector.php';


class UserPage extends GetData{

    private $daten;
    public $User_Id = 0;
    public $Visitor_Id = 0;
    public $page = false;
    public $PageUser = false;
    public $design = false;
    public $passwort = false;
    public $expire_time = 120;

    public function __construct($match){
        $this->daten = $match;

        if (isset ( $_SESSION ['UserId'] )) {
            $this->Visitor_Id = $_SESSION ['UserId'];
        }
        $user = new Db_User();
        $this->User_Id = $user->GetUserByName($this->daten['Name']);
        $this->User_Id = $this->User_Id->id;

        if(!is_numeric($this->User_Id)){
            HTTP::redirect ( BASE_URL . '/UserNotFound');
        }
        if(isset($this->daten['Page'])){
            $this->getpage = $this->daten['Page'];
            $this->PageUser = $this->daten['Name'];
        }
    }

    public function getWort(RainTPL $design){
        $this->design = $design;
        $pageurl = '/user/'.$this->daten['Name'].'/';
         
        if($this->design->post->isSetValue() > 0 || $this->design->get->isSetValue() > 0){
            $this->expire_time = 0;
        }
        
        if(isset($this->getpage)){
            $pageurl .=  $this->getpage ;
        }
        if (!$this->User_Id)
        throw new Exception (CHANGE_CONTROLLER);

        $GetStaticPage = new Db_GetStaticPage();
        $Modul = new Db_Modul();
        $this->design->assign ('userpage','/user/'.$this->daten['Name'].'/');
        $this->design->assign ('url',$pageurl);
        $this->design->assign ('user_id',$this->User_Id);

        if($this->Visitor_Id != 0){
            $this->design->assign ('visitorID',$this->Visitor_Id);
        }

        if(isset($this->getpage)){
            $page = $GetStaticPage->getPage($this->User_Id,$this->daten['Page']);

            if(!$page){
                $page = $Modul->getModul($this->User_Id,$this->daten['Page']);
            }              
            
	        $page->underpage = 0;
	        
            if(isset($page->Controller) && $page->aktiv == 0){
                include_once 'app/modul/'.$page->Controller.'.php';
                 
                $modul = new $page->Controller($this);
                 
                if($this->design->get->isSetValue() > 0 || $this->design->post->isSetValue() > 0 || $this->design->file->isSetValue() > 0){
                    $page = $modul->getRequest($design, $page);
                     
                    if(!empty($modul->error)){
                        $this->design->assign ('modulError',$modul->error);
                    }
                }
                 
                $modulcontent= $modul->Main();
                //$this->design->assign ('modul',$modulcontent);
                $this->design->assign ('modulLink',$page->link);
                 
                if($page->css){
                    $this->design->assign ('modulcss',$page->css);
                }
            }
            if(!$page){
                HTTP::redirect ( BASE_URL . '/user/'.$this->daten['Name']);
            }
        }else{
            $this->design->assign ('css',"loginscreen");
            $this->design->assign ('js',"jsLoginScreen");
            $page = new Db_GetStaticPage();
            $page = $page->getPageMain($this->User_Id,$this->page);

            if(!$page){
                if( $cache = $this->design->cache($this->obj->ln.'/User/Page/nopage', $expire_time = 0 ) )
                    echo $cache;
                else{
                    $this->design->draw($this->obj->ln.'/User/Page/nopage');
                }
                exit;
            }
        }
        $passwort = NULL;      
        $passwort = $this->design->post->PagePasswort;

        $userdesign = new Db_User();
        $userdesign = $userdesign->getSelectetDesign($this->User_Id);

###### Page mit Passwort 
        if($page->zugriff == 2){
            if(isset($_SESSION["PagePasswort"]) && $page->passwort == $_SESSION["PagePasswort"] )
            {
                $this->passwort = true;
            }else{
                if($page->passwort == $passwort){
                    $this->passwort = true;
                    $_SESSION["PagePasswort"] = $page->passwort;
                }else{
                    unset($_SESSION["PagePasswort"]);
                }
            }   
        }
                 
        if(!$this->passwort && $page->zugriff == 2 && $page->aktiv == 0){
            if( $cache = $this->design->cache('template/passwort', 0) )
                echo $cache;
            else{
                $this->design->assign ('js',"jsLoginScreen");
                $this->design->assign ('css',"loginscreen");
                
                $this->design->draw('template/passwort');
            }
            exit;
        }  
        
        if($page->aktiv == 0 || $this->passwort && $page->aktiv == 0 ){
        	$visitors = new Db_user_page_visitor();
        	$visitors->setVisitor($this->User_Id,$page->ExpId,$page->url,$this->Visitor_Id);

        	if( $cache = $this->design->cache( 'template/'.$userdesign->design.'/index', $this->expire_time,$page->underpage.$page->ExpId) ){
                echo $cache;
            }else{                    
                $Widget = new Db_user_page_widget();
    
                $spalte1 = $Widget->GetUserWidgetByUseridAndSpalte($this->User_Id, 1);
                $spalte2 = $Widget->GetUserWidgetByUseridAndSpalte($this->User_Id, 2);
    
                foreach ($spalte2 as $key => $widget){
                    if(isset($widget->modul->modul_id)){
                        include_once 'app/modul/'.$widget->modul->Controller.'.php';
                        $widgetmodul =  new $widget->modul->Controller($this);
                        $this->design->assign ($widget->modul->Controller,$widgetmodul->Widget());
                    }
                }
    
                foreach ($spalte1 as $key => $widget){
                    if(isset($widget->modul->modul_id)){
                        include_once 'app/modul/'.$widget->modul->Controller.'.php';
                        $widgetmodul =  new $widget->modul->Controller($this);
                        $this->design->assign ($widget->modul->Controller,$widgetmodul->Widget());
                    }
                }
                $this->design->assign ('spalte1',$spalte1);
                $this->design->assign ('spalte2',$spalte2);
        }
            
            ###### Page Meta und Content   
            if(isset($page->text)){
                $this->design->assign ('content',$page->text);
            }
            if(isset($page->name)){
                $this->design->assign ('pagename',$page->name);
            }
            if(isset($page->meta_robots)){
                $this->design->assign ('meta_robots',$page->meta_robots);
            }
            if(isset($page->meta_keys)){
                $this->design->assign ('meta_keys',$page->meta_keys);
            }
            if(isset($page->meta_beschreibung)){
                $this->design->assign ('meta_beschreibung',$page->meta_beschreibung);
            }
            if(isset($page->slogen)){
                $this->design->assign ('slogen',$page->slogen);
            }
            
            $this->design->draw( 'template/'.$userdesign->design.'/index' );
        }else{
            if( $cache = $this->design->cache( 'template/notaktiv', 0) )
                echo $cache;
            else{
                $this->design->assign ('js',"jsLoginScreen");
                $this->design->assign ('css',"loginscreen");
                $this->design->draw( 'template/notaktiv' );
            }
        }
    }

}
?>